let pageMixin = {
  data () {
    return {
      page: 1,
      pageSize: 20,
      noMoreData: false,
      isRequesting: false,
      list: [],
      pageTime:0,
    }
  },
  methods: {
    //  加载第一页
    loadFirstPage (clearList = false) {
      // if (this.isRequesting) {
      //   mpvue.stopPullDownRefresh();
      //   return
      // }

      if (clearList) {
        this.list = []
      }
      this.pageTime = new Date().getTime()
      this.page = 1
      // this.pageConfig.pageNo = 1
      this.loadData(this.pageTime)
      // this.loadData()
    },
    //  加载下一页
    loadMore () {
      if (this.noMoreData || this.isRequesting) {
        return
      }

      this.loadData()
    },
    loadData (time) {
      this.isRequesting = true
      mpvue.showLoading({
        title: "加载中...",
        mask: true
      })

      this.customerLoadData().then(rs => {
        if (time && time != this.pageTime) {
          console.log('数据防覆盖机制生效！')
          return
        }
        let listData = this.customerData(rs)
        
        //  判断是否还能加载下一页
        if (listData.list.length < this.pageSize) {
          this.noMoreData = true
        } else {
          this.noMoreData = false
        }

        //  是第一页的话先将列表制空
        if (this.page == 1) {
          this.list = []
          mpvue.pageScrollTo({
            scrollTop: 0,
            duration: 0
          })
        }

        if (listData.list.length) {
          this.list.push(...listData.list)
        }

        this.page++
        this.isRequesting = false

        mpvue.stopPullDownRefresh();
        mpvue.hideLoading();

        this.customerAfterLoadData('success', rs)

      }, err => {
        if (time && time != this.pageTime) {
          console.log('err 数据防覆盖机制生效！')
          return
        }
        this.isRequesting = false
        mpvue.stopPullDownRefresh();
        mpvue.hideLoading();

        this.customerAfterLoadData('fail', err)
      })
    },

    customerLoadData () {},
    customerData (rs) {
      return {
        list: rs.rows || []
      }
    },
    customerAfterLoadData() {}
  }
}

export default pageMixin
